package com.example.education.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.education.Bean.CollegeSubjects;
import com.example.education.Bean.StudentInfo;
import com.example.education.Mapper.CollegeSubjectsMapper;
import com.example.education.service.CollegeSubjectsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 姚
 * @since 2023-03-27
 */
@Service
@Transactional
public class CollegeSubjectsServiceImpl extends ServiceImpl<CollegeSubjectsMapper, CollegeSubjects> implements CollegeSubjectsService {


    @Autowired
    private CollegeSubjectsMapper collegeSubjectsMapper;

    @Override
    public int insert(CollegeSubjects collegeSubjects) {
        if (collegeSubjects.getUid() == null) {
            return collegeSubjectsMapper.insert(collegeSubjects);
        } else {
            UpdateWrapper<StudentInfo> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("uid", collegeSubjects.getUid());


            updateWrapper.set("grade", collegeSubjects.setUniversityName(""));


            return collegeSubjectsMapper.update(collegeSubjects, updateWrapper);
        }
    }

    @Override

    public int delete(Integer uid) {
        return collegeSubjectsMapper.delete(uid);
    }

    @Override
    public int update(CollegeSubjects collegeSubjects) {
        return 0;
    }

    @Override
    public ArrayList<CollegeSubjects> select(String collegename) {//根据学校名字查询这个学校所有的相关专业
        return collegeSubjectsMapper.select(collegename);
    }

    @Override
    public CollegeSubjects selectByScore(Integer score) {
        return null;
    }

    @Override
    public ArrayList<CollegeSubjects> find(StudentInfo studentInfo) {
        int grade = studentInfo.getGrade();
        ArrayList<CollegeSubjects> list = new ArrayList<CollegeSubjects>();

        //如果grade+3有相应学校 将这个学校加入到list中
        for (int i = grade + 2; i <= grade + 3; i++) {
            ArrayList<CollegeSubjects> list1 = collegeSubjectsMapper.selectByGrade(i);
            if (list1.size() != 0) {
                list.addAll(list1);
                break;
            }
        }


        //如果grade+1有相应学校 将这个学校加入到list中
        for (int i = grade; i <= grade + 1; i++) {
            ArrayList<CollegeSubjects> list1 = collegeSubjectsMapper.selectByGrade(i);
            if (list1.size() != 0) {
                list.addAll(list1);
                break;
            }
        }

        //如果grade-3有相应学校 将这个学校加入到list中
        for (int i = grade - 2; i <= grade - 1; i++) {
            ArrayList<CollegeSubjects> list1 = collegeSubjectsMapper.selectByGrade(i);
            if (list1.size() != 0) {
                list.addAll(list1);
                break;
            }
        }

        return list;
    }


}
